package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SystemUserService service;

    @Override
    public CaptchaVo getCaptcha() {
        //图片长宽字数
        SpecCaptcha captcha = new SpecCaptcha(130, 48, 4);
        //验证码全是数字格式
        captcha.setCharType(Captcha.TYPE_DEFAULT);

        //生成验证码字符串
        String code = captcha.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString().replaceAll("-","");
        //生成图片验证码base64加密处理 todo base64 : (万物->字符串->万物)
        String image = captcha.toBase64();
        //存储生成的验证码
        redisTemplate.opsForValue().set(key,code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        CaptchaVo captchaVo = new CaptchaVo(image,key);
        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {
        //1.检查验证码-是否为空-是否正确-是否过期
        //拿取存储redis中验证码的value
        String redisValue = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        //为空判断
        if (ObjectUtils.isEmpty(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //正确判断-大小写不敏感
        if (!redisValue.equalsIgnoreCase(loginVo.getCaptchaCode())){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //过期判断
        if (ObjectUtils.isEmpty(redisValue)){
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //2.检查账号和密码-账号是否正确-是否可用
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<SystemUser>().eq(SystemUser::getUsername, loginVo.getUsername());
        SystemUser systemUser = service.getOne(queryWrapper);
        if (systemUser == null){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //可用判断
        if (systemUser.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.APP_ACCOUNT_DISABLED_ERROR);
        }
        // -密码是否正确
        String md5Hex = DigestUtils.md5Hex(loginVo.getPassword());
        if (!md5Hex.equals(systemUser.getPassword())){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //3.生成token
        return JwtUtil.createToken(systemUser.getId(),systemUser.getUsername());
    }
}
